<!-- 完成 -->
<template>

	<fui-form label-width="auto">

		<custom-form-item-card>
			<!-- <fui-form-item label="实际报修地址" label-width="auto" arrow asterisk>
				<template #right>
					<fui-text type="primary" :text="labelActualRepairPlace" :size="32"
						@click="choseActualRepairPlace"></fui-text>
				</template>
			</fui-form-item> -->
			<fui-form-item label="实际故障描述" label-width="auto" asterisk>
				<template #vertical>
					<fui-textarea :borderTop="false" placeholder="填写故障内容" v-model="formData.faultRemark"></fui-textarea>
				</template>
			</fui-form-item>
			<fui-form-item label="实际故障分类" label-width="auto" asterisk arrow :bottom-border="false">
				<template #right>
					<fui-text type="primary" :text="labelFaultType" :size="32" @click="choseFaultType"></fui-text>
				</template>
			</fui-form-item>
		</custom-form-item-card>

		<custom-form-item-card>
			<fui-form-item label="解决办法" label-width="auto">
				<template #vertical>
					<fui-textarea :borderTop="false" placeholder="你是如何解决此故障" v-model="formData.makeWay"></fui-textarea>
				</template>
			</fui-form-item>
		</custom-form-item-card>

		<custom-form-item-card>
			<fui-form-item label="实际工时" :bottomBorder="false" label-width="auto">
				<fui-input v-model="formData.realTime" :borderBottom="false" number type="number" textAlign="right"
					:padding="['0']" placeholder="请输入" maxlength="6"></fui-input>
				<template #right>
					<fui-text text="小时" type="primary"></fui-text>
				</template>
			</fui-form-item>
		</custom-form-item-card>

		<custom-form-item-card>
			<fui-form-item label="附件" label-width="auto" :bottom-border="false">
				<template #right>
					<custom-button-upload @change="onUploadFile"></custom-button-upload>
				</template>
			</fui-form-item>
			<custom-file-list :list="fileList" @del="onDelFile"></custom-file-list>
		</custom-form-item-card>

		<!-- <custom-form-item-card>
			<fui-form-item label="资产名称" label-width="auto">
				<template #right>
					<fui-text text="资产名称资产名称" size="32" color="#000000"></fui-text>
				</template>
			</fui-form-item>
			<fui-form-item label="资产位置" label-width="auto" arrow :bottom-border="false">
				<template #right>
					<fui-text type="primary" :text="labelAssetAddr" :size="32" @click="choseAssetAddr"></fui-text>
				</template>
			</fui-form-item>
		</custom-form-item-card> -->
	</fui-form>
</template>

<script>
	import {
		defineComponent
	} from "vue"
	import {
		mapWritableState,
		mapState,
		mapActions
	} from 'pinia'
	import {
		useHandleWorkOrder_Complete
	} from "../../../../store/modules/handleWorkOrder_Complete"
	import {
		base64ToFile,
		customValue,
		DeptSelectChoseType,
		toDeptSelectPage,
		toDicSelectPage
	} from "../../../../utils/util"
	import {
		FormDeptStoreKey,
		FormDicStoreKey
	} from "../../../../store/sotreKey"
	import {
		DIC_KEY
	} from "../../../../config/dicKey-config"
	import {
		useDictStore
	} from "../../../../store/modules/dic"
	import {
		useDeptStore
	} from "../../../../store/modules/dept"
	import {
		WorkOrderApi,
		WorkOrderMyIndexApi
	} from "../../../../api/workOrder"
	import form from '@/components/firstui/fui-validator/fui-validator'
	import {
		FileApi
	} from "../../../../api/file"
	import _ from 'lodash'
	export default defineComponent({
		data() {
			return {

			}
		},
		computed: {
			...mapWritableState(useHandleWorkOrder_Complete, ['formData', 'fileList']),
			...mapState(useDictStore, ['dicStoreKey']),
			...mapState(useDeptStore, ['deptStoreKey']),
			// 实际报修位置
			labelActualRepairPlace() {
				const {
					label,
					value
				} = this.dicStoreKey[FormDicStoreKey.MyWorkOrderHandleCompleteActualRepairPlace.key]
				this.formData.actualRepairPlace = value
				return label
			},
			// 实际故障分类
			labelFaultType() {
				const {
					value
				} = this.dicStoreKey[FormDicStoreKey.MyWorkOrderHandleCompleteFaultType.key]
				if (value.length) {
					const v = _.map(value, item => JSON.parse(item))
					this.formData.faultType = v[0].value
					return v[0].label
				}
				this.formData.faultType = null
				return '请选择'
			},
			// 资产位置
			labelAssetAddr() {
				const {
					label,
					value
				} = this.deptStoreKey[FormDeptStoreKey.MyWorkOrderHandleCompleteAssetAddr.key]
				this.formData.assetAddr = customValue(value).value
				return label
			},
		},
		emits: ['disabled'],
		methods: {
			...mapActions(useHandleWorkOrder_Complete, ['onDelFile']),
			...mapActions(useDictStore, ['clearDicStoreKeyValue']),
			onUploadFile(f) {
				this.fileList = [...this.fileList, ...f]
			},
			// 实际故障描述
			choseActualRepairPlace() {
				toDicSelectPage(FormDicStoreKey.MyWorkOrderHandleCompleteActualRepairPlace.key, DIC_KEY
					.obs_repair_place, false)
			},
			// 实际故障分类
			choseFaultType() {
				toDicSelectPage(FormDicStoreKey.MyWorkOrderHandleCompleteFaultType.key, DIC_KEY.obs_fault_type,
					false)
			},
			// 资产位置
			choseAssetAddr() {
				toDeptSelectPage(FormDeptStoreKey.MyWorkOrderHandleCompleteAssetAddr.key, DeptSelectChoseType
					.selectDept, false)
			},
			async submit(orderNum, cb) {
				const rules = [
					// 	{
					// 	name: "actualRepairPlace",
					// 	rule: ["required"],
					// 	msg: ["请选择报修地址"]
					// },
					{
						name: "faultRemark",
						rule: ["required"],
						msg: ["请输入故障描述"]
					}, {
						name: "faultType",
						rule: ["required"],
						msg: ["请选择故障分类"]
					}
				];
				try {
					this.formData.orderNum = orderNum

					let {
						isPassed,
						errorMsg
					} = form.validator(this.formData, rules)
					if (isPassed) {
						this.$emit('disabled', true)
						if (this.fileList.length) {
							for (let i of this.fileList) {
								try {
									let f = base64ToFile(i.base64)
									const result = await FileApi.upload('image', f)
									const r = await FileApi.uploadWechatFile(result.mediaId)
									this.formData.processEnclosure.push(r.data)
								} catch (err) {
									console.error(err);
								}

							}
						}
						await WorkOrderMyIndexApi.complete(this.formData)
						this.fui.toast('提交成功!')
						useHandleWorkOrder_Complete().$reset()
						this.clearDicStoreKeyValue(FormDicStoreKey.MyWorkOrderHandleCompleteFaultType.key)
						cb && cb()

					} else {
						this.$emit('disabled', false)
						this.fui.toast(errorMsg)
					}
				} catch (err) {
					this.$emit('disabled', false)
					console.error(err);
					this.fui.toast(err)

				}
			}
		}

	})
</script>

<style>
</style>